package com.fcat.springboot.mqtt.emqx;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PushCallback implements MqttCallbackExtended {
    private static final Logger logger = LoggerFactory.getLogger(PushCallback.class);
    @Autowired
    private MqttConfiguration mqttConfiguration;

    @Autowired
    private MessageDealService messageDealService;

    @Override
    public void connectionLost(Throwable cause) {
        logger.info("连接断开");
        mqttConfiguration.start();
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
       // logger.info("deliveryComplete---------" + token.isComplete());
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) {
        logger.info("topic："+ topic);
        logger.info("message："+ new String(message.getPayload()));
        logger.info("Qos："+message.getQos());
        messageDealService.messageArrived(topic,message);
    }

    @Override
    public void connectComplete(boolean b, String s) {
        logger.info("connectComplete");
    }
}